﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Castle.ActiveRecord;
using System.IO;
using System.Web;

namespace Windvoice.Albums
{
    public partial class Album : ActiveRecordBase<Album>
    {
        public Photo[] GetPhotoes()
        {
            return Photo.FindAll().Where(p => p.AlbumID == this.ID).ToArray();
        }

        public int GetPhotoCount()
        {
            return Photo.FindAll().Where(p => p.AlbumID == this.ID).Count();
        }

        public Photo GetFacePhoto()
        {
            var query = from p in Photo.FindAll() where p.AlbumID == this.ID && p.ID == this.FacePhoto select p;
            return query.SingleOrDefault();
        }

        public static Photo[] GetUploadedPhoto(int albumID)
        {
            return Photo.FindAll().Where(p => p.AlbumID == albumID).Where(p => p.Flag == -1).ToArray();
        }

        public static Album[] GetList(int pageIndex, int pageSize, out int recordCount)
        {
            recordCount = FindAll().Count();
            return FindAll().OrderByDescending(a=>a.CreateTime).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToArray();
        }

        public override void Delete()
        {
            try {
                string RelativeOriginalPath = String.Format("/files/photo/{0}", this.ID);
                Directory.Delete(HttpContext.Current.Server.MapPath(RelativeOriginalPath), true);
            }
            catch { }
            base.Delete();
        }
    }
}
